package com.yanze.customviewdemo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class Bezier2 extends View {

    private Paint mPaint;
    private int centerX,centetY;
    private PointF start,end,control1,control2;
    private boolean mode = true; // 控制控制点状态

    public void setMode(boolean mode) {
        this.mode = mode;
    }

    public Bezier2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(8);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);

        start = new PointF(0, 0);
        end = new PointF(0, 0);
        control1 = new PointF(0, 0);
        control2 = new PointF(0, 0);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX = w / 2;
        centetY = h / 2;

        // 初始数据点和控制点位置
        start.x = centerX - 200;
        start.y = centetY;
        end.x = centerX + 200;
        end.y = centetY;
        control1.x = centerX - 100;
        control1.y = centetY + 100;
        control2.x = centerX + 100;
        control2.y = centetY - 100;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制数据点和控制点
        mPaint.setColor(Color.GRAY);
        mPaint.setStrokeWidth(20);
        canvas.drawPoint(start.x,start.y,mPaint);
        canvas.drawPoint(end.x,end.y,mPaint);
        canvas.drawPoint(control1.x,control1.y,mPaint);
        canvas.drawPoint(control2.x,control2.y,mPaint);

        // 绘制辅助线
        mPaint.setStrokeWidth(3);
        canvas.drawLine(start.x, start.y, control1.x, control1.y, mPaint);
        canvas.drawLine(control1.x, control1.y, control2.x, control2.y, mPaint);
        canvas.drawLine(control2.x,control2.y,end.x,end.y,mPaint);

        //绘制三阶Bezier曲线
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(8);
        Path path = new Path();
        path.moveTo(start.x, start.y);
        path.cubicTo(control1.x, control1.y, control2.x, control2.y, end.x, end.y);
        canvas.drawPath(path,mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mode) {
            control1.x = event.getX() < 0 ? 0 : (event.getX() > getWidth() ? getWidth() : event.getX());
            control1.y = event.getY() < 0 ? 0 : (event.getY() > getHeight() ? getHeight() : event.getY());
        }else{
            control2.x = event.getX() < 0 ? 0 : (event.getX() > getWidth() ? getWidth() : event.getX());
            control2.y = event.getY() < 0 ? 0 : (event.getY() > getHeight() ? getHeight() : event.getY());
        }
        invalidate();
        return true;
    }
}
